package com.huishu.commons.utlis;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 日期格式，年月日时分，例如：2000-12-30 12:00，2008-08-08 20:08
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";


    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";


//    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";

    /**
     * 日期格式，年月日，用横杠分开，例如：2006-12-25，2008-08-08
     */
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    public static final String DATE_FORMAT_YY_MM_DD = "yyMMdd";

    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_T_HH_MI_SS_SSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    /**
     * 格式化Date时间
     *
     * @param time       Date类型时间
     * @param timeFromat String类型格式
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat) {
        DateFormat dateFormat = new SimpleDateFormat(timeFromat);
        return dateFormat.format(time);
    }

    /**
     * 格式化String时间
     *
     * @param time       String类型时间
     * @param timeFromat String类型格式
     * @return 格式化后的Date日期
     */
    public static Date parseStrToDate(String time, String timeFromat) {
        if (time == null || time.equals("")) {
            return null;
        }

        Date date = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(timeFromat);
            date = dateFormat.parse(time);
        } catch (Exception e) {

        }
        return date;
    }

    /**
     * 日期加天
     *
     * @param date
     * @param day
     * @return
     */
    public static Date plusDay(Date date, int day) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        nowTime.add(Calendar.DATE, day);
        return nowTime.getTime();
    }


    /**
     * 日期加秒
     *
     * @param date
     * @param seconds
     * @return
     */
    public static Date plusSeconds(Date date, int seconds) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        nowTime.add(Calendar.SECOND, seconds);
        return nowTime.getTime();
    }


    /**
     * 日期加小时
     *
     * @param date
     * @param hour
     * @return
     */
    public static Date plusHour(Date date, int hour) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        nowTime.add(Calendar.HOUR, hour);
        return nowTime.getTime();
    }

    /**
     * 日期加分钟
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date plusMinutes(Date date, int minutes) {
        Calendar nowTime = Calendar.getInstance();
        nowTime.setTime(date);
        nowTime.add(Calendar.MINUTE, minutes);
        return nowTime.getTime();
    }

    /**
     * 获取某日期的年份
     *
     * @param date
     * @return
     */
    public static Integer getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获取某日期的月份
     *
     * @param date
     * @return
     */
    public static Integer getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取某日期的日数
     *
     * @param date
     * @return
     */
    public static Integer getDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DATE);//获取日
        return day;
    }

    /**
     * 根据出生日期计算年龄
     *
     * @param birthday
     * @return
     */
    public static int getAge(Date birthday) {
        if (birthday == null) {
            return 0;
        }
        Integer year = DateUtils.getYear(birthday);
        Integer month = DateUtils.getMonth(birthday);
        Integer day = DateUtils.getDay(birthday);
        return getAgeByBirthDay(year + "", month + "", day + "");
    }

    /**
     * 计算分钟时间差
     *
     * @return
     */
    public static Integer minuteDifference(Date start, Date end) {
        return (int) ((end.getTime() - start.getTime()) / (1000 * 60));
    }

    /**
     * 计算日期相差天数
     *
     * @return
     */
    public static int dayDifference(Date date1, Date date2) {
        return (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
    }

    /**
     * 时间大于比较 start > end
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean compareGt(Date startDate, Date endDate) {
        return startDate.getTime() > endDate.getTime();
    }

    public static int getAgeByBirthDay(String birth_Year, String birth_Mouth, String birth_Day) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YYYY_MM_DD);
        //String date = "2008-01-01";/
        //得到当前的年份
        Date date = new Date();
        String cYear = sdf.format(date).substring(0, 4);
        String cMouth = sdf.format(date).substring(5, 7);
        String cDay = sdf.format(date).substring(8, 10);

        //年月日比较后得到年龄
        int age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
        if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth)) < 0) {
            age = age - 1;
        } else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth)) == 0) {
            if ((Integer.parseInt(cDay) - Integer.parseInt(birth_Day)) > 0) {
                age = age - 1;
            } else {
                age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
            }
        } else if ((Integer.parseInt(cMouth) - Integer.parseInt(birth_Mouth)) > 0) {
            age = Integer.parseInt(cYear) - Integer.parseInt(birth_Year);
        }
        return age;
    }

    public static Date randomDate() {
        String date = parseDateToStr(new Date(), DATE_FORMAT_YYYY_MM_DD);
        return randomDate(date + " 10:10:10", date + " 21:50:10");
    }

    public static Date randomDate(String beginDate, String endDate) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = format.parse(beginDate);// 构造开始日期
            Date end = format.parse(endDate);// 构造结束日期
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
            if (start.getTime() >= end.getTime()) {
                return null;
            }
            long date = random(start.getTime(), end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long random(long begin, long end) {
        long rtn = begin + (long) (Math.random() * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return random(begin, end);
        }
        return rtn;
    }


    /**
     * 获得指定时间加减参数后的日期(不计算则输入0)
     *
     * @param date        指定日期
     * @param year        年数，可正可负
     * @param month       月数，可正可负
     * @param day         天数，可正可负
     * @param hour        小时数，可正可负
     * @param minute      分钟数，可正可负
     * @param second      秒数，可正可负
     * @param millisecond 毫秒数，可正可负
     * @return 计算后的日期
     */
    public static Date addDate(Date date, int year, int month, int day, int hour, int minute, int second, int millisecond) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, year);//加减年数
        c.add(Calendar.MONTH, month);//加减月数
        c.add(Calendar.DATE, day);//加减天数
        c.add(Calendar.HOUR, hour);//加减小时数
        c.add(Calendar.MINUTE, minute);//加减分钟数
        c.add(Calendar.SECOND, second);//加减秒
        c.add(Calendar.MILLISECOND, millisecond);//加减毫秒数
        return c.getTime();
    }

    /**
     * 两个时间之间相差距离多少天
     *
     * @return 相差天数
     */
    public static long getDistanceDays(Date str1, Date str2) {
        long days;
        long time1 = str1.getTime();
        long time2 = str2.getTime();
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        days = diff / (1000 * 60 * 60 * 24);
        return days;
    }


    /**
     * 根据天数或月数计算结束日期
     *
     * @param unit      单位 0月 1年
     * @param number    数量
     * @param startDate 开始时间
     * @return
     */
    public static Date getEndDateByDays(Integer unit, Integer number, Date startDate) {
        //计算总月数
        Integer amount = unit == 0 ? number : 12 * number;

        //计算到期时间
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.add(Calendar.MONTH, amount);
        return c.getTime();
    }


    public static void main(String[] args) {
        Date plusDay = plusDay(new Date(), -10);
        Date date = new Date();
        System.out.println(parseDateToStr(date, DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        System.out.println(parseDateToStr(plusDay, DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
        System.out.println(compareGt(date, plusDay));
//        System.out.println(parseDateToStr(, DATE_TIME_FORMAT_YYYY_MM_DD_T_HH_MI_SS_SSSZ));
    }

    /**
     * 日期增加天数获取未来的日期
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDays(Date date, Integer days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    public static Long theNextDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        //当前时间与晚上十二点的毫秒差
        return ((calendar.getTimeInMillis() - System.currentTimeMillis()) / 1000);
        //当前时间与晚上十二点的分钟差
       // Long secondTimeOut = (calendar.getTimeInMillis() - System.currentTimeMillis()) / (1000 * 60);
       // System.out.println("当前时间与晚上十二点的毫秒差" + timeOut);
       // System.out.println("当前时间与晚上十二点的分钟差" + secondTimeOut);
    }
}
